<?php

namespace app\model\goods;

use app\base\BaseModel;
use app\model\activity\ActivityModel;
use app\model\agency\AgencyModel;
use app\model\distributor\DistributorModel;
use app\model\user\UserModel;

/**
 * 模型-GoodsTypeModel
 * Class GoodsTypeModel
 * @package app\model
 */
class GoodsStoreModel extends BaseModel
{
    protected $name = 'goods_store';

    /**
     * 范围查询-keyword
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('g.goods_title', "%$keyword%");
        }
    }

    /**
     * 格式化数据
     * @param $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function format(&$data)
    {
        $data['add_time_str'] = date("Y-m-d H:i", $data['add_time']);
        $remark = "";
        switch ($data['operate']) {
            case "system":
                if (!empty($data['target_id']) && $data['change'] == "add") {
                    $distributor_model = new DistributorModel();
                    $distributor = $distributor_model->getInfo($data['target_id'], 'name');
                    $remark = "渠道商：" . $distributor['name'] . "(" . $data['target_id'] . ")";
                }
                $data['operate_str'] = "平台库存变化";
                break;
            case "agency":
                $data['operate_str'] = "代理商库存变化";
                if (!empty($data['target_id'])) {
                    $agency_model = new AgencyModel();
                    $agency = $agency_model->getInfo($data['target_id'], 'agency_title');
                    $remark = "代理商：" . $agency['agency_title'] . "(" . $data['target_id'] . ")";
                }
                break;
            case "activity":
                $data['operate_str'] = "活动库存变化";
                if (!empty($data['target_id'])) {
                    $activity_model = new ActivityModel();
                    $activity = $activity_model->getInfo($data['target_id'], 'name');
                    $remark = "活动：" . $activity['name'] . "(" . $data['target_id'] . ")";
                }
                break;
            case "store":
                if (!empty($data['target_id']) && $data['change'] == "add") {
                    $distributor_model = new DistributorModel();
                    $distributor = $distributor_model->getInfo($data['target_id'], 'name');
                    $remark = "渠道商：" . $distributor['name'] . "(" . $data['target_id'] . ")";
                }
                $data['operate_str'] = "平台库存变化";
                break;
        }
        if (!empty($data['user_id'])) {
            $user_model = new UserModel();
            $user = $user_model->getInfo($data['user_id'], 'nick_name');
            $remark .= " 购买用户：" . $user['nick_name'];
        }
        $data['remark_str'] = $remark;
        if ($data['change'] == "add") {
            $data['change_str'] = "增加";
        } else {
            $data['change_str'] = "减少";
        }

    }

    /**
     * @param $id
     * @param $operate
     * @param $aid
     * @param $keyword
     * @param $page
     * @param $limit
     * @param $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getListData($id, $operate, $aid, $keyword, $page, $limit, $field)
    {
        $models = $this
            ->alias("s")
            ->leftJoin("t_goods_common g", "s.attribute_ids=g.attribute_ids")
            ->field($field)
            ->where('g.goods_id', $id)
            ->where('s.operate', $operate);
        if (!empty($aid)) {
            $models = $models->where('s.target_id', $aid);
        }
        $dataList = $models->keyword($keyword)
            ->order('s.add_time', 'desc')
            ->order('s.id', 'desc')
            ->paginate(['page' => $page, 'list_rows' => $limit], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $dataList = $dataList->items();
        foreach ($dataList as &$value) {
            $this->format($value);
        }
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList];
        return $ret;
    }

    /**
     * 增加库存记录
     * @param array $storage
     * @param bool $is_all
     * @return bool
     * @throws \Exception
     */
    public function addStorage($storage = [], $is_all = false)
    {
        if ($is_all) {
            $ret = $this->saveAll($storage);
        } else {
            $ret = $this->save($storage);
        }
        if ($ret !== false) {
            return true;
        }
        return false;
    }

}
